package mulegame;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.border.BevelBorder;
import javax.swing.border.LineBorder;
import javax.swing.border.MatteBorder;
import javax.swing.border.TitledBorder;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.IOException;
import javax.swing.AbstractAction;
import javax.swing.JTextArea;

/**
 * Creates the Pub Screen with players and different buying options. will add logic and actionlisteners later
 * @author Briam
 *
 */

public class PubScreen extends TimedScreen {
    private JLabel lblTimer;
    private int timeleft;
    private JTextArea txtrPlayer;
    
	/**
	 * Create the Pub screen with player scores, choices to gamble or return to Town
	 * path images of different options with action listeners 
	 */
    
	public PubScreen(GraphicsControl ctrl, int duration) {
        super(ctrl, duration);
        

		setSize(new Dimension(900, 600));
		setBorder(new MatteBorder(4, 4, 4, 4, (Color) new Color(0, 0, 0)));
		setLayout(null);

		int numOfPlayers = control.getNumPlayers();
		
		JLabel lblMainTitle = new JLabel("Coffee Mule");
		lblMainTitle.setBounds(175, 11, 563, 51);
		lblMainTitle.setForeground(new Color(165, 42, 42));
		lblMainTitle.setHorizontalAlignment(SwingConstants.CENTER);
		lblMainTitle.setFont(new Font("Batang", Font.BOLD, 45));
		add(lblMainTitle);
		
		JLabel lblLogoSMALL = new JLabel("");
		lblLogoSMALL.setBounds(20, 11, 97, 93);
		lblLogoSMALL.setBorder(new LineBorder(new Color(0, 0, 0), 2));
		lblLogoSMALL.setIcon(new ImageIcon("src/mulegame/Images/LogoSmall.jpg"));
		add(lblLogoSMALL);

		lblTimer = new JLabel("XX Seconds");
		lblTimer.setForeground(Color.RED);
		lblTimer.setBorder(new LineBorder(Color.BLACK, 2));
		lblTimer.setFont(new Font("Times New Roman", Font.PLAIN, 29));
		lblTimer.setHorizontalAlignment(SwingConstants.CENTER);
		lblTimer.setBounds(708, 10, 168, 83);
		add(lblTimer);
		
		JPanel pubBorder = new JPanel();
		pubBorder.setBorder(new TitledBorder(null, "Coffee Pub",
				TitledBorder.LEADING, TitledBorder.TOP, null, null));
		pubBorder.setBounds(223, 83, 471, 295);
		add(pubBorder);
		pubBorder.setLayout(null);
		
		JLabel lblNewLabel = new JLabel("");
		lblNewLabel.setBorder(new LineBorder(new Color(0, 0, 0)));
		lblNewLabel.setIcon(new ImageIcon("src/mulegame/Images/PubAll.JPG"));
		lblNewLabel.setBounds(20, 23, 430, 263);
		pubBorder.add(lblNewLabel);
		
		JPanel panelAction = new JPanel();
		panelAction.setBorder(new TitledBorder(null, "Choose:", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panelAction.setBounds(697, 116, 181, 262);
		add(panelAction);
		panelAction.setLayout(null);
		
		JLabel lblGamble = new JLabel("Gamble");
		lblGamble.setBounds(6, 20, 169, 38);
		panelAction.add(lblGamble);
		lblGamble.setHorizontalAlignment(SwingConstants.CENTER);
		lblGamble.setForeground(Color.BLACK);
		lblGamble.setFont(new Font("MV Boli", Font.BOLD, 18));
		
		JLabel lvlReturn = new JLabel("Return to Town");
		lvlReturn.setBounds(6, 130, 169, 38);
		panelAction.add(lvlReturn);
		lvlReturn.setHorizontalAlignment(SwingConstants.CENTER);
		lvlReturn.setForeground(Color.BLACK);
		lvlReturn.setFont(new Font("MV Boli", Font.BOLD, 18));
		
		JButton btnGamble = new JButton(new GambleAction());
		btnGamble.setText("I'm Feeling Lucky!");
		btnGamble.setBorder(new BevelBorder(BevelBorder.RAISED, null, null, null, null));
		btnGamble.setFont(new Font("Times New Roman", Font.PLAIN, 19));
		btnGamble.setBorderPainted(true);
		btnGamble.setBounds(12, 57, 155, 50);
		panelAction.add(btnGamble);
		
		JButton btnReturn = new JButton(new BackToTownAction());
		btnReturn.setText("Take me back!");
		btnReturn.setBorder(new BevelBorder(BevelBorder.RAISED, null, null, null, null));
		btnReturn.setBorderPainted(true);
		btnReturn.setFont(new Font("Times New Roman", Font.PLAIN, 19));
		btnReturn.setBounds(16, 170, 152, 50);
		panelAction.add(btnReturn);
            
        timeleft = (duration/1000);
        timeleft++;
        timerTick();
	}

	/**
	 * makes timer tick down and dispalys 
	 */
	@Override
	public void timerTick(){
	    //decrement timeleft and update the timer lable
	    Integer time = timeleft--;
	    lblTimer.setText(time.toString() + " Seconds");
        invalidate();//this should be the last call in this method
	}

	/**
     * 
     */
	@Override
	public void timerTimeOut(){
	     //do nothing, MainScreen's timer will take care of changing the screen   
	}
	
	/**
     * creates action listener for gamble action
     *  @author AREA 29
     *
     */
	private class GambleAction extends AbstractAction{
	    
	    @Override
        public void actionPerformed(ActionEvent e) {
            control.gambleClicked();
        }
    }

	/**
     * creates action listener for going back to town button
     * @author AREA 29
     *
     */
    private class BackToTownAction extends AbstractAction{
        @Override
        public void actionPerformed(ActionEvent e){
        	doClickSound();
            control.backToTownScreen();   
        }
    }
    
    /**
     * controls the clicking sound by playing an audiofile
     */
    public void doClickSound() {
    	String soundName = "src/mulegame/Sounds/click.wav";    
    	AudioInputStream audioInputStream = null;
		try {
			audioInputStream = AudioSystem.getAudioInputStream(new File(soundName).getAbsoluteFile());
		} catch (UnsupportedAudioFileException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	Clip clip = null;
		try {
			clip = AudioSystem.getClip();
		} catch (LineUnavailableException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	try {
			clip.open(audioInputStream);
		} catch (LineUnavailableException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	clip.start();
    }    
}